/**
 * related to detail.ui
 * 
 * @Author : chenglm
 * @Timestamp : 2016-07-16
 */
var page = sm("do_Page");
var app = sm("do_App");
var do_External = sm("do_External");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var orderUtil = require("url/order");
var qqHttp = require("helper/http");
var openHelper = require("helper/open");
var helperOrder = require("helper/order");
var do_LinearLayout_item = ui("do_LinearLayout_item");
var do_LinearLayout_1 = ui("do_LinearLayout_1");

var labelOrderStatus = ui("labelOrderStatus");
var iconBg = ui("iconBg");
var do_Label_consignee = ui("do_Label_consignee");
var do_Label_phone = ui("do_Label_phone");
var do_Label_address = ui("do_Label_address");
var do_Label_seller = ui("do_Label_seller");
var do_Label_total = ui("do_Label_total");
var do_ALayout_freight = ui("do_ALayout_freight");
var do_Label_freight = ui("do_Label_freight");
var do_ALayout_sales = ui("do_ALayout_sales");
var do_Label_sales = ui("do_Label_sales");
var do_Label_community = ui("do_Label_community");

var do_Label_orderNo = ui("do_Label_orderNo");
var do_Label_strNewAt = ui("do_Label_strNewAt");
var do_Label_payStatusAt = ui("do_Label_payStatusAt");
var do_Label_orderStatusAt = ui("do_Label_orderStatusAt");
var do_Label_deliveryAt = ui("do_Label_deliveryAt");
var do_Label_deliveryInfo = ui("do_Label_deliveryInfo");
var do_Label_returnPay = ui("do_Label_returnPay");

var button_phone = ui("button_phone");
var label_remark = ui("label_remark");
var label_payTypeStr = ui("label_payTypeStr");
var do_GridView_coupon=ui("do_GridView_coupon");
var do_Button_service = ui("do_Button_service");
var do_Label_sendName = ui("do_Label_sendName");
//andriod返回键
page.on("back", function() {
	 app.closePage();
});

var headerAout = ui("do_ALayout_header");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
	 app.closePage();
});
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "订单详情"
})
header.refreshData();

//页面装载完成后，开始初始化工作
page.on("loaded", function(){
	var orderNo = page.getData().orderNo;
	getData(orderNo);
});
var order;
function getData(orderNo) {
	qqHttp.post(orderUtil.url.base.shopOrderDetail, {orderNo:orderNo}, function(data) {
		order = data.order;
		do_Label_sendName.text = data.sendDeliveryName;
		do_Label_sendName.tag = data.sendDeliveryPhone;
		var delivery = data.delivery;
		var itemList = data.itemList;
		labelOrderStatus.text = order.orderStatusText;
		label_remark.text = order.remark;
		if (order.orderStatus == 3) {
			iconBg.source="source://image/order/close.png";
		} else if (order.orderStatusText == "待付款") {
			iconBg.source="source://image/order/wait.png";
		} else if (order.orderStatusText == "交易完成") {
			iconBg.source="source://image/order/wc.png";
		}
		/*收货地址*/
		if(null!=delivery){
			if(delivery.type == 4){
				do_Label_consignee.text = "联系人："+delivery.consignee;
				do_Label_phone.text = delivery.phone;
				do_Label_address.text = "自提地址："+delivery.address;
			}else{
				do_Label_consignee.text = "收货人："+delivery.consignee;
				do_Label_phone.text = delivery.phone;
				do_Label_address.text = "收货地址："+delivery.address;
				if(delivery.type == 1){//卖家发货类型，配送费
					do_ALayout_freight.visible = true;
					do_Label_freight.text = "￥" + delivery.freight;
				}
			}
			do_Label_community.text = "所在社区："+order.communityName;
		}
		if(order.subsidy > 0){
			do_ALayout_sales.visible = true;
			do_Label_sales.text = "-￥"+order.subsidy;
		}else{
			do_ALayout_sales.visible = false;
		}
		button_phone.tag = data.sellerPhone;
		/*订单商品*/
		for(var i=0;i<itemList.length;i++) {
			var addItem = do_LinearLayout_item.add("item"+i, "source://view/order/detailTemplate.ui");
			var orderItemUi = ui("item"+i);
			orderItemUi.bindData(hashdata);
			hashdata.addData({
				"orderType" : order.orderType,
			    "productId" : itemList[i].productId,
			    "productName" :itemList[i].name + " " + itemList[i].productSpecName,
			    "productPrice" : "￥" + itemList[i].price,
			    "productCount" : itemList[i].quntity,
			    "productImg" : itemList[i].pathImg
			});
			orderItemUi.refreshData();
		}
		do_Label_seller.text = order.buyerNick;
		do_Label_total.text = "￥" + order.total;
		do_Label_orderNo.text = "订单号："+order.orderNo;
		do_Label_strNewAt.text = "下单时间："+order.strNewAt;
		if (order.payStatus == 2) {
			do_Label_payStatusAt.visible = true;
			do_Label_payStatusAt.text = "付款时间："+order.strPayStatusAt; // 付款时间
		}
		if (order.deliveryStatus != 1) {
			do_Label_deliveryAt.visible = true;
			do_Label_deliveryAt.text = "发货时间："+order.strDeliveryStatusAt; // 发货时间		
			if (delivery.type == 1) {
				do_Label_deliveryInfo.visible = true;
				if (delivery.hasNeedDelivery == 1) {
					do_Label_deliveryInfo.text = "物流运单："+ delivery.deliveryName +' '+delivery.deliveryNo;
				} else if (delivery.hasNeedDelivery == 2) {
					if (delivery.sendDeliveryType == 1) {
						do_Label_deliveryInfo.text = "配送方式：商家配送";
					} else if (delivery.sendDeliveryType == 2) {
						do_Label_deliveryInfo.text = "配送方式：圈圈配送";
					}
				}
			}
		}
		if (order.returnPayStatus != 1) {
			do_Label_returnPay.visible = true;
			do_Label_returnPay.text = "申请退款："+order.strReturnPayStatusAt;
		}
		if (order.orderStatus == 2) {
			if (order.strOrderStatusAt) {
				do_Label_orderStatusAt.visible = true;
			}
			do_Label_orderStatusAt.text = "完成时间："+order.strOrderStatusAt; // 完成时间
		} else if (order.orderStatus == 3) {
			do_Label_orderStatusAt.visible = true;
			do_Label_orderStatusAt.text = "取消时间："+order.strOrderStatusAt;
		}
		if (order.payType == 1) {
			label_payTypeStr.text = "货到付款";
		} else {
			label_payTypeStr.text = "在线支付";
		}
		do_GridView_coupon.items=data.orderCouponVoList;
		ui("do_LinearLayout_2").redraw();
	});
}

button_phone.on("touch", function(data){
	if (do_Label_sendName.tag != null && do_Label_sendName.tag != "") {
		do_External.openDial(do_Label_sendName.tag);
	}else{
		nf.toast("尚未有配送");
	}
});

do_Button_service.on("touch", function(data){
	if (do_Label_phone.text != null && do_Label_phone.text != "") {
		do_External.openDial(do_Label_phone.text);
	}
});